package com.neusoft.lic.rescue.menudialogtest;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private TextView tvinfo,tvmenu;
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        registerForContextMenu(tvmenu);
    }

    private void init() {
        tvinfo=(TextView)findViewById(R.id.textView);
        tvmenu=(TextView)findViewById(R.id.textView2);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater=getMenuInflater();
        inflater.inflate(R.menu.menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
            case R.id.menu1:
                tvinfo.setText("menu1");
                showdialog();
                break;
            case R.id.menu2:
                tvinfo.setText("menu2");
                break;
            case R.id.menu3:
                tvinfo.setText("menu3");
                showdialog();
                break;
            case R.id.menu4:
                tvinfo.setText("menu4");
                break;
            case R.id.menu5:
                tvinfo.setText("menu5");
                break;
            case R.id.menu6:
                tvinfo.setText("menu6");
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        MenuInflater inflater=getMenuInflater();
        inflater.inflate(R.menu.menu2,menu);
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu7:
                tvinfo.setText("上下文菜单1");
                break;
            case R.id.menu8:
                tvinfo.setText("上下文菜单2");
                break;
        }
        return super.onContextItemSelected(item);
    }


    private void showdialog() {
        final View view=getLayoutInflater().inflate(R.layout.dialoglayout,null);
        AlertDialog dialog=new AlertDialog.Builder(this)
                .setTitle("对话框标题")
                .setIcon(R.mipmap.ic_launcher)
                .setView(view)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        EditText etname=(EditText)view.findViewById(R.id.editText);
                        String msg=etname.getText().toString();
                        Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
                        tvinfo.setText(msg);
                    }
                })
                .setNegativeButton("返回", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                .create();
        dialog.show();
  }
}
